﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace QCCM2
{
    public class QCSchedule
    {
        private int materialID;
        private string materialName;
        private int qcAttribID;
        public string qcAttribName;
 
        private string hour;
        private string day;
        private int groupID;
        private bool qcScheduleDisabled;


        public int MaterialID
        {
            get { return materialID; }
            set { materialID = value; }
        }

        public string MaterialName
        {
            get { return materialName; }
            set { materialName = value; }
        }

        public int QCAttribID
        {
            get { return qcAttribID; }
            set { qcAttribID = value; }
        }


        public string QCAttribName
        {
            get { return qcAttribName; }
            set { qcAttribName = value; }
        }


        public int GroupID
        {
            get { return groupID; }
            set { groupID = value; }
        }


        public string Hour
        {
            get { return hour; }
            set { hour = value; }
        }

        public string Day
        {
            get { return day; }
            set { day = value; }
        }

        public bool QCScheduleDisabled
        {
            get { return qcScheduleDisabled; }
            set { qcScheduleDisabled = value; }
        }

        String connectionString = @"Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True";

        public List<QCSchedule> GetAllQCSchedules()
        {
            List<QCSchedule> QCScheduleList = new List<QCSchedule>();
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string queryString = "SELECT* From dbo.QCSchedule"; 
                
                try
                {
                   
                    SqlCommand command = new SqlCommand(queryString, connection);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        QCSchedule qcs = new QCSchedule();

                        qcs.materialID = reader.GetInt32(0);
                        qcs.qcAttribID = reader.GetInt32(1);
                        qcs.hour = reader.GetString(2);
                        qcs.day = reader.GetString(3);
                        qcs.groupID = reader.GetInt32(4);
                        qcs.qcScheduleDisabled = reader.GetBoolean(5);

                        QCScheduleList.Add(qcs);
                    }
                    connection.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                return QCScheduleList;
            }
        }

        public string AddQCSchedule(int materialID, int qcAttribID, string hour, string day, int groupID, bool qcScheduleDisabled)
        {
            bool success = false;
            String connectionString = @"Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {

                try
                {

                    string queryString = "insert into [dbo].[QCSchedule](materialID, qcAttribID, groupID, hour, day, qcScheduleDisabled) values(@Hour, @Day, @Disabled) WHERE MaterialID ='" + materialID + "' AND QCAttribID= '" + qcAttribID + "'";
                    
                    connection.Open();
                    SqlCommand command = new SqlCommand(queryString, connection);
                    {
                        command.Parameters.AddWithValue("@MaterialID", SqlDbType.VarChar).Value = Convert.ToString(materialID);
                        command.Parameters.AddWithValue("@QcAttribID", SqlDbType.VarChar).Value = Convert.ToString(qcAttribID);
                        command.Parameters.AddWithValue("@Hour", SqlDbType.DateTime).Value = Convert.ToDateTime(hour);
                        command.Parameters.AddWithValue("@Day", SqlDbType.VarChar).Value = Convert.ToString(day);
                        command.Parameters.AddWithValue("@GroupID", SqlDbType.VarChar).Value = Convert.ToString(groupID);
                        command.Parameters.Add("@Disabled", SqlDbType.Bit).Value = Convert.ToBoolean(qcScheduleDisabled);

                        int rows = command.ExecuteNonQuery();

                        if (rows == 1)
                        {
                            success = true;
                        }

                    }
                }

                catch (SqlException ex)
                {
            
                    Console.WriteLine(ex.ToString());
                }
            
                return "materialID" + success;
            }
        }


        public string UpdateQSchedule(int materialID, int qcAttribID, string hour, string day, int groupID, bool qcScheduleDisabled)
        {
           
            try
            {
                string result = "";

                SqlConnection cs = new SqlConnection("Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
                string Query = "UPDATE dbo.QCSchedule SET Hour= @Hour, Day= @Day  WHERE MaterialID ='" + materialID + "' AND QCAttribID= '" + qcAttribID + "'";
                SqlCommand cm = new SqlCommand(Query, cs);

                cs.Open();
                cm.Parameters.AddWithValue("@Hour", hour);
                cm.Parameters.AddWithValue("@Day", day);
                cm.Parameters.AddWithValue("@Disabled", qcScheduleDisabled);

                cm.ExecuteNonQuery();

                result = "The" + materialName + " schedule has been updated.";
                cs.Close();
            }
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                Console.WriteLine(sqlException.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
         return "materialID";

        }

        public string DeleteQCSchedule(int materialID, int qcAttribID, int groupID)
        {
            string result = "";

            try
            {
                SqlConnection cs = new SqlConnection("Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
                string Query = "DELETE FROM [dbo].[MaterialQCAttrib] WHERE MaterialID= @MaterialID AND QCAttribID= @QCAttribID";
                SqlCommand cm = new SqlCommand(Query, cs);

                cs.Open();
                cm.ExecuteNonQuery();

                result = "The" + materialName + " Schedule Successfully Deleted.";
                cs.Close();
            }

            catch (FileNotFoundException fnfe)
            {
                result = fnfe.Message;
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return result;
            //string Error = string.Empty;
            //Log LogUpdate = new Log();
            //LogUpdate.ToString(materialId, "", ex.Message, Error);

        }

    }
}

